package com.kitty.game.task.service.taskHandler;

import com.kitty.common.utils.SpringUtils;
import com.kitty.game.config.NPC;
import com.kitty.game.config.TaskSet;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.i18n.I18nId;
import com.kitty.game.i18n.I18nIdDataPool;
import com.kitty.game.role.model.Role;
import com.kitty.game.task.model.product.TaskType;
import com.kitty.game.utils.AsktaoUtil;
import com.kitty.game.utils.Const;
import com.kitty.common.model.Reason;
import com.kitty.mina.message.MessagePusher;
import org.springframework.stereotype.Component;

/**人物飞升处理类*/
@Component
public class RoleFlyTaskHandler extends TaskHandler {
    /**人物飞升任务的任务id*/
    private int taskId = 96;
    /**结婴需要金钱*/
    private int reqMoney = 0;
    /**结婴需要元宝*/
    private int reqGold = 0;

    @Override
    public TaskType getTaskType() {
        return TaskType.ROLE_FLY;
    }

    public void handleRoleLevelUp(Role role, short level) {
        if (level != Const.MIN_LEVEL_NO_CHILD) {
            return ;
        }

        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId, role);
        acceptTask(role, taskSet);
    }

    /**列出结婴选项*/
    public void listJieYing(Role role, NPC npc) {
        String moneyStr = AsktaoUtil.getMoneyFormat(reqMoney);
        String content = I18nIdDataPool.getI18nContent(I18nId.PMT_2300, moneyStr, reqGold);
        SpringUtils.getNpcService().sendNpcContent(role, npc, content);
    }

    /**选择结元婴*/
    public void chooseYuanYing(Role role, NPC npc) {
        boolean check = checkResource(role);
        if (!check) {return ;}

        substractResource(role);
        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId, role);
        forceResetTask(role, taskSet);
        /**元婴*/
        SpringUtils.getRoleService().getChild(role, (byte)1);
    }

    /**选择结血婴*/
    public void chooseXueYing(Role role, NPC npc) {
        boolean check = checkResource(role);
        if (!check) {return ;}

        substractResource(role);
        TaskSet taskSet = SpringUtils.getTaskService().getTaskSet(taskId, role);
        forceResetTask(role, taskSet);

        /**血婴*/
        SpringUtils.getRoleService().getChild(role, (byte)2);
    }

    /**检测资源*/
    private boolean checkResource(Role role) {
        if (role.getMoney() < reqMoney && role.getGold() < reqGold) {
            MessagePusher.notify2Player(role, I18nId.PMT_2301);
            return false;
        }
        return true;
    }

    /**扣除资源*/
    private void substractResource(Role role)  {
        if (role.getMoney() >= reqMoney && role.getGold() >= reqGold) {
            SpringUtils.getRoleService().subtractMoney(role, reqMoney, Reason.ROLE_JIEYING);
            SpringUtils.getRoleService().subtractGold(role, reqGold, Reason.ROLE_JIEYING);
            return ;
        }else{
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("游戏币或元宝不足!"));
            throw new RuntimeException("飞升游戏币或元宝不足!");
        }
    }
}
